# coding:utf-8

import os
basedir = os.path.abspath(os.path.dirname(__file__))

from apscheduler.jobstores.sqlalchemy import SQLAlchemyJobStore


class Config:
    SECRET_KEY = os.environ.get('SECRET_KEY') or 'hard to guess string'
    SSL_DISABLE = False
    SQLALCHEMY_COMMIT_ON_TEARDOWN = False
    SQLALCHEMY_RECORD_QUERIES = True
    SQLALCHEMY_TRACK_MODIFICATIONS = False
    #MAIL_SERVER = 'smtp.googlemail.com'
    #MAIL_PORT = 587
    #MAIL_USE_TLS = True
    #MAIL_USERNAME = os.environ.get('MAIL_USERNAME')
    #MAIL_PASSWORD = os.environ.get('MAIL_PASSWORD')
    #FLASKY_MAIL_SUBJECT_PREFIX = '[Flasky]'
    #FLASKY_MAIL_SENDER = 'Flasky Admin <flasky@example.com>'
    FLASKY_ADMIN = os.environ.get('FLASKY_ADMIN')
    FLASKY_POSTS_PER_PAGE = 20
    FLASKY_FOLLOWERS_PER_PAGE = 50
    FLASKY_COMMENTS_PER_PAGE = 30
    FLASKY_SLOW_DB_QUERY_TIME = 0.5
    # py3.6遇到的诡异情况，需要设置 csrf 为 False
    # 否则 FlaskForm 实例化会失败，是否 CSRF_TOKEN 没设置引起的？
    WTF_CSRF_ENABLED = False

    # rabbitmq开关
    RABBITMQ = False

    # 上传录音路径
    MEDIA_PATH = os.path.join(basedir, 'app/static/media/')

    # lnlcc虚拟号服务商相关信息
    CORP_HOST = os.environ.get('CORP_HOST') or ''
    CORP_KEY = os.environ.get('CORP_KEY') or ''
    CORP_SECRET = os.environ.get('CORP_SECRET') or ''

    # 斗地主那边的 secret_key
    GAME_SECRET = os.environ.get('GAME_SECRET') or ''

    @staticmethod
    def init_app(app):
        pass


class DevelopmentConfig(Config):
    DEBUG = True
    RABBITMQ = False
    # 开发数据库
    #SQLALCHEMY_DATABASE_URI = os.environ.get('DEV_DATABASE_URL') or \
    #    'mysql+pymysql://root:@127.0.0.1/wph_www_dev?charset=utf8'

    # 线上数据库拷贝，用于功能开发调试
    SQLALCHEMY_DATABASE_URI = os.environ.get('DEV_DATABASE_URL') or \
        'mysql+pymysql://root:@127.0.0.1/wph_www_prod?charset=utf8'

    MEDIA_PATH = os.environ.get('MEDIA_PATH') or \
        os.path.join(basedir, 'app/static/media/')


class TestingConfig(Config):
    TESTING = True
    SQLALCHEMY_DATABASE_URI = os.environ.get('TEST_DATABASE_URL') or \
        'mysql+pymysql://root:@127.0.0.1/wph_www_prod_test?charset=utf8'
    WTF_CSRF_ENABLED = False

    MEDIA_PATH = os.environ.get('MEDIA_PATH') or \
        os.path.join(basedir, 'app/static/media/')


class ProductionConfig(Config):
    # 生产上需要把rabbitmq打开
    RABBITMQ = True
    SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or \
        'sqlite:///' + os.path.join(basedir, 'data.sqlite')

    # @classmethod
    # def init_app(cls, app):
    #     Config.init_app(app)
    #
    #     # email errors to the administrators
    #     import logging
    #     from logging.handlers import SMTPHandler
    #     credentials = None
    #     secure = None
    #     if getattr(cls, 'MAIL_USERNAME', None) is not None:
    #         credentials = (cls.MAIL_USERNAME, cls.MAIL_PASSWORD)
    #         if getattr(cls, 'MAIL_USE_TLS', None):
    #             secure = ()
    #     mail_handler = SMTPHandler(
    #         mailhost=(cls.MAIL_SERVER, cls.MAIL_PORT),
    #         fromaddr=cls.FLASKY_MAIL_SENDER,
    #         toaddrs=[cls.FLASKY_ADMIN],
    #         subject=cls.FLASKY_MAIL_SUBJECT_PREFIX + ' Application Error',
    #         credentials=credentials,
    #         secure=secure)
    #     mail_handler.setLevel(logging.ERROR)
    #     app.logger.addHandler(mail_handler)

    MEDIA_PATH = os.environ.get('MEDIA_PATH') or \
        os.path.join(basedir, 'app/static/media/')


class OutboundConfig(Config):
    """
    外呼与websocket配置
    """
    DEBUG = True
    RABBITMQ = False

    # 开发数据库
    #SQLALCHEMY_DATABASE_URI = os.environ.get('DEV_DATABASE_URL') or \
    #    'mysql+pymysql://root:@127.0.0.1/wph_www_dev?charset=utf8'

    # 线上数据库拷贝，用于功能开发调试
    SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or \
        'mysql+pymysql://root:@127.0.0.1/wph_www_prod?charset=utf8'

    JOBS = [
        {
            'id': 'outbound_job',
            'func': 'app.ccws:scheduler_jobs.outbound_job',
            'trigger': 'interval',
            'seconds': 10,
            #'trigger': {
            #    'type': 'cron',
            #    'day_of_week':"mon-sat",
            #    'hour':'9-19',
            #    'minute':'*',
            #    'second': '*/10',
            #    'timezone': 'Asia/Shanghai',
            #},
        },
        #{
        #    'id': 'test_job',
        #    'func': 'app.ccws:scheduler_jobs.test_job',
        #    'args':(1, 2),
        #    'trigger': 'interval',
        #    'seconds': 10,
        #},
    ]

    #SCHEDULER_JOBSTORES = {
    #    'default': SQLAlchemyJobStore(
    #        'sqlite:///' + os.path.join(basedir, 'flask_context.db')
    #    )
    #}

    #SCHEDULER_API_ENABLED = True

    MEDIA_PATH = os.environ.get('MEDIA_PATH') or \
        os.path.join(basedir, 'app/static/media/')


config = {
    'development': DevelopmentConfig,
    'testing': TestingConfig,
    'production': ProductionConfig,
    'outbound': OutboundConfig,

    'default': DevelopmentConfig
}
